package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.qiantu.api.entity.ProductSchemeConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;

import org.jetbrains.annotations.NotNull;

public class DeviceComposeAdapter extends AppAdapter<ProductSchemeConfig> {
    private int schemeValue;
    private int selectPosition;
    public DeviceComposeAdapter(@NonNull @NotNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public DeviceComposeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new DeviceComposeAdapter.ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {
        private LinearLayout backLayout;
        private final TextView tvScheme;
        private final TextView tvSchemeInfo;
        private final CheckBox btnChoose;


        private ViewHolder() {
            super(R.layout.device_compose_item);
            backLayout = findViewById(R.id.btn_scheme);
            tvScheme = findViewById(R.id.tv_scheme);
            tvSchemeInfo = findViewById(R.id.tv_scheme_info);
            btnChoose = findViewById(R.id.btn_choose);
        }


        @Override
        public void onBindView(int position) {
            ProductSchemeConfig productSchemeConfig = getItem(position);
            if(productSchemeConfig.getValue() == schemeValue){
                selectPosition = position;
                btnChoose.setChecked(true);
            }else{
                btnChoose.setChecked(false);
            }
            tvScheme.setText(productSchemeConfig.getName());
            tvSchemeInfo.setText(productSchemeConfig.getBriefing());
            backLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    schemeValue = productSchemeConfig.getValue();
                    selectPosition = position;
                    onSelectChanged(schemeValue);
                }
            });
        }
    }


    public void setSchemeValue(int  selected) {
        if(selected == 0){
            selected = 1;
        }
        this.schemeValue = selected;
    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
    }

    public int getSelectPosition() {
        return selectPosition;
    }

    public void onSelectChanged(int selected) {
        notifyDataSetChanged();
    }
}
